package com.example.demoJDBC;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author qw
 * @Description
 * @date 19/4/2022
 */
@Slf4j
@Component
public class WebService {
    public static void main(String[] args) {
        try {
            String s = WebServicePost();
            System.out.println(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static String WebServicePost() throws Exception {
        OutputStream out = null ;
        InputStreamReader in = null ;
        BufferedReader br = null ;
        String result = null ;
        StringBuilder sb = null ;
        try {
            HttpURLConnection connection = getConnection();
            //5. 组织SOAP协议数据，发送给服务端
            String soapXML = getXML(1, 2);
            //6. 写soap协议数据
            out = connection.getOutputStream();
            out.write(soapXML.getBytes());
            //7. 接收服务端的响应
            int responseCode = connection.getResponseCode();
            //8. 响应码为200则读数据
            if (responseCode == 200) {
                in = new InputStreamReader(connection.getInputStream());
                br = new BufferedReader(in);
                sb = new StringBuilder();
                String temp = null;
                while (null != (temp = br.readLine())) {
                    sb.append(temp);
                }
            }
        }catch(Exception e){
            throw new Exception(e.getMessage());
        }finally{
                out.close();
                in.close();
                br.close();
        }
        //返回的也是soap协议的响应数据
        if(sb != null && !"".equals(sb)){
            result = sb.toString();
            result = result.substring(result.indexOf("<GetSumResult>")+14,result.indexOf("</GetSumResult>"));
        }
        return result;
    }

    /**
     *
     *  通过地址获得连接对象
     *
     */
    private static synchronized HttpURLConnection getConnection() throws Exception {
        HttpURLConnection connection = null ;
        try {
            //1. 创建服务地址 ，能正常访问到的wsdl地址，但是此处需要去掉?wsdl eg：http://XXX/uapws/service/xxx?wsdl  - > http://XXX/uapws/service/xxx
            URL realURL = new URL("http://127.0.0.1:8080/soap/test");
            connection = (HttpURLConnection) realURL.openConnection();
            //2. 设置发送方式：POST必须大写
            connection.setRequestMethod("POST");
            //3. 设置数据格式：Content-type
            connection.setRequestProperty("Content-type", "application/json");
            //4. 设置输入，新创建的connection默认是没有写权限的，doOutput值默认为false
            connection.setDoOutput(true);
            return connection;
        } catch (IOException e) {
            log.error(e.getMessage());
            throw new Exception("获取发送地址连接出错");
        }
    }


    /**
     * soap协议组装
     * soap格式解释:  除了<soap:Body></soap:Body>里面的内容，其余外面的内容都是固定的 ，<soap:Body>标签下面第一个标签是方法标签
     * 比如发布的webservice的方法是returnErrorLog , 其中xmlns:nsl属性的值是地址 http://XXX/XXX ，地址怎么设置值呢？
     * 找到能访问的wsdl地址去正常访问，找到 xsd:schema 标签 ，然后后面的xmlns:ns属性的值就是这个地址
     * 再接着是传入参数，在页面上找到<xsd:element name="returnErrorLog">标签，然后查看下面的<xsd:sequence>标签，这个标签下的<xsd:element>
     * 的属性name的值就是传入参数，如果有多个<xsd:element>标签就有多个传入参数
     *
     * **/
    public static String getXML(Integer beginTime , Integer endTime){
        String soapXML = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:tem=\"http://tempuri.org/\">\n" +
                "   <soap:Header/>\n" +
                "   <soap:Body>\n" +
                "      <tem:GetSum>\n" +
                "         <!--Optional:-->\n" +
                "         <tem:number1>"+beginTime+"</tem:number1>\n" +
                "         <!--Optional:-->\n" +
                "         <tem:number2>"+endTime+"</tem:number2>\n" +
                "      </tem:GetSum>\n" +
                "   </soap:Body>\n" +
                "</soap:Envelope>";
        return soapXML;
    }

}

